package com.cbgolf.oa.util;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import com.cbgolf.oa.R;
import com.cbgolf.oa.app.App;
import com.scwang.smartrefresh.header.CircleHeader;
import com.scwang.smartrefresh.header.DeliveryHeader;
import com.scwang.smartrefresh.header.DropboxHeader;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.header.PhoenixHeader;
import com.scwang.smartrefresh.header.StoreHouseHeader;
import com.scwang.smartrefresh.header.TaurusHeader;
import com.scwang.smartrefresh.header.WaveSwipeHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.scwang.smartrefresh.layout.header.BezierRadarHeader;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import java.lang.reflect.Field;
import org.xutils.view.annotation.ViewInject;

/**
 * @author lq 2017/9/26
 */

public class ViewUtils {

  //    /**
//     * 注入主布局文件
//     *
//     * @param activity
//     */
//    private static void injectContentView(Activity activity)
//    {
//        Class<? extends Activity> clazz = activity.getClass();
//        // 查询类上是否存在ContentView注解
//        ContentView contentView = clazz.getAnnotation(ContentView.class);
//        if (contentView != null)// 存在
//        {
//            int contentViewLayoutId = contentView.value();
//            try
//            {
//                Method method = clazz.getMethod(METHOD_SET_CONTENTVIEW,
//                        int.class);
//                method.setAccessible(true);
//                method.invoke(activity, contentViewLayoutId);
//            } catch (Exception e)
//            {
//                e.printStackTrace();
//            }
//        }
//    }
//    public static IRefreshWorker worker;

//    public interface IRefreshWorker {
//        void onRefreshBegin(PtrFrameLayout frame);
//
//        void onLoadMoreBegin(PtrFrameLayout frame);
//    }

  /**
   * 解析注解
   */
  public static void autoInjectAllField(Object cls, View view) {
    try {
      Class<?> clazz = cls.getClass();
      Field[] fields = clazz.getDeclaredFields();//获得Activity中声明的字段
      for (Field field : fields) {
        // 查看这个字段是否有我们自定义的注解类标志的
        if (field.isAnnotationPresent(ViewInject.class)) {
          ViewInject inject = field.getAnnotation(ViewInject.class);
          int id = inject.value();
          if (id > 0) {
            field.setAccessible(true);
            field.set(cls, view.findViewById(id));//给我们要找的字段设置值
          }
        }
      }
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    } catch (IllegalArgumentException e) {
      e.printStackTrace();
    }
  }

  /**
   * 解析注解
   */
  public static void autoInjectAllField(Object cls, Activity act) {
    try {
      Class<?> clazz = cls.getClass();
      Field[] fields = clazz.getDeclaredFields();//获得Activity中声明的字段
      for (Field field : fields) {
        // 查看这个字段是否有我们自定义的注解类标志的
        if (field.isAnnotationPresent(ViewInject.class)) {
          ViewInject inject = field.getAnnotation(ViewInject.class);
          int id = inject.value();
          if (id > 0) {
            field.setAccessible(true);
            field.set(cls, act.findViewById(id));//给我们要找的字段设置值
          }
        }
      }
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    } catch (IllegalArgumentException e) {
      e.printStackTrace();
    }
  }


  public static void setVisible(View view, boolean isVisible) {
    if (null != view) {
      view.setVisibility(isVisible ? View.VISIBLE : View.GONE);
    }
  }

  public static void setInVisible(View view, boolean isInVisible) {
    if (null != view) {
      view.setVisibility(isInVisible ? View.INVISIBLE : View.VISIBLE);
    }
  }

  public static void setEnable(View view, boolean enable) {
    if (null != view) {
      view.setEnabled(enable);
    }
  }

  public static void setChecked(CheckBox cb, boolean check) {
    if (null != cb) {
      cb.setChecked(check);
    }
  }

  public static void setSwipRefreshLayout(Activity context, SwipeRefreshLayout refreshLayout) {
    // 设置下拉圆圈上的颜色，蓝色、绿色、橙色、红色
    refreshLayout.setColorSchemeResources(R.color.bg_blue, R.color.text_gray,
        R.color.white, R.color.bg_blue);
//        refreshLayout.setColorSchemeResources(android.R.color.holo_blue_light,
//                android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light);
    refreshLayout.setDistanceToTriggerSync(300);// 设置手指在屏幕下拉多少距离会触发下拉刷新
    refreshLayout.setSize(SwipeRefreshLayout.LARGE);
    refreshLayout
        .setProgressBackgroundColorSchemeColor(ContextCompat.getColor(context, R.color.white90));
  }

  /**
   * 球的进度类似时钟，弹起的球
   */
  public static void setRefreshBezierHeader(Context context, RefreshLayout refreshLayout) {
    BezierRadarHeader header = new BezierRadarHeader(context);
    header.setPrimaryColor(ContextCompat.getColor(context, R.color.bg_blue));
    refreshLayout.setRefreshHeader(header);
    setRefreshFooter(context, refreshLayout);
  }

  /**
   * 弹起的球
   */
  public static void setRefreshCircleHeader(Context context, RefreshLayout refreshLayout) {

    CircleHeader header = new CircleHeader(context);
    header.setPrimaryColors(ContextCompat.getColor(
        context, R.color.bg),
        ContextCompat.getColor(
            context, R.color.bg_blue));
    refreshLayout.setRefreshHeader(header);
    setRefreshFooter(context, refreshLayout);
  }

  /**
   * 加载文字，支持英文
   */
  public static void setRefreshTextHeader(Context context, RefreshLayout refreshLayout) {
    StoreHouseHeader header = new StoreHouseHeader(context);
    header.setPrimaryColors(ContextCompat.getColor(
        context, R.color.bg),
        ContextCompat.getColor(
            context, R.color.bg_blue));
    header.initWithString("LOADING");
    refreshLayout.setRefreshHeader(header);
    setRefreshFooter(context, refreshLayout);
  }

  /**
   * 加载文字，支持英文
   */
  public static void setRefreshTextHeader(Context context, RefreshLayout refreshLayout,
      String loadText) {
    StoreHouseHeader header = new StoreHouseHeader(context);
    header.setPrimaryColors(ContextCompat.getColor(
        context, R.color.bg),
        ContextCompat.getColor(
            context, R.color.bg_blue));
    header.initWithString(Util.isNull(loadText) ? "LOADING" : loadText);
    refreshLayout.setRefreshHeader(header);
    setRefreshFooter(context, refreshLayout);
  }

  /**
   * 水滴效果
   */
  public static void setRefreshWaterHeader(Context context, RefreshLayout refreshLayout) {
    WaveSwipeHeader header = new WaveSwipeHeader(context);
    header.setColorSchemeColors(ContextCompat.getColor(context, R.color.white)
//                ,
//                ContextCompat.getColor(context, R.color.bg_blue),
//                ContextCompat.getColor(context, R.color.text_black),
//                ContextCompat.getColor(context, R.color.bg_yellow),
//                ContextCompat.getColor(context, R.color.bg_blue)
    );
    header.setPrimaryColors(ContextCompat.getColor(context, R.color.LightSkyBlue));
    refreshLayout.setRefreshHeader(header);//
    setRefreshFooter(context, refreshLayout);
  }

  /**
   * 水滴效果
   */
  public static void setRefreshWaterHeader(Context context, RefreshLayout refreshLayout,
      int color) {

    WaveSwipeHeader header = new WaveSwipeHeader(context);

    header.setColorSchemeColors(
        ContextCompat.getColor(context, R.color.bg_blue)
    );
    header.setPrimaryColors(color);
    refreshLayout.setRefreshHeader(header);//
    setRefreshFooter(context, refreshLayout);
  }

  /**
   * 热气球
   */
  public static void setRefreshDeliveryHeader(Context context, RefreshLayout refreshLayout) {
    DeliveryHeader header = new DeliveryHeader(context);
    refreshLayout.setRefreshHeader(header);//
    setRefreshFooter(context, refreshLayout);
  }


  /**
   * //弹起的球,中间有进度转圏
   */
  public static void setRefreshMaterialHeader(Context context, RefreshLayout refreshLayout) {
    MaterialHeader header = new MaterialHeader(context);
    refreshLayout.setRefreshHeader(header.setShowBezierWave(true));//
    setRefreshFooter(context, refreshLayout);
  }

  /**
   * 盒子
   */
  public static void setRefreshDropboxHeader(Context context, RefreshLayout refreshLayout) {
    DropboxHeader header = new DropboxHeader(context);
    refreshLayout.setRefreshHeader(header);//
    setRefreshFooter(context, refreshLayout);
  }

  /**
   * 蓝天白云
   */
  public static void setRefreshTaurusHeader(Context context, RefreshLayout refreshLayout) {
    TaurusHeader header = new TaurusHeader(context);
    refreshLayout.setRefreshHeader(header);//
    setRefreshFooter(context, refreshLayout);
  }

  /**
   * 房子太阳
   */
  public static void setRefreshPhoenixHeader(Context context, RefreshLayout refreshLayout) {
    PhoenixHeader header = new PhoenixHeader(context);
    refreshLayout.setRefreshHeader(header);//
    setRefreshFooter(context, refreshLayout);
  }

  /**
   * 传统的
   */
  public static void setRefreshClassicsHeader(Context context, RefreshLayout refreshLayout) {
    ClassicsHeader header = new ClassicsHeader(context);
    refreshLayout.setRefreshHeader(header);//
    setRefreshFooter(context, refreshLayout);

  }

  public static void setRefreshFooter(Context context, RefreshLayout refreshLayout) {
    BallPulseFooter footer = new BallPulseFooter(context);
    footer.setSpinnerStyle(SpinnerStyle.Scale);
    footer.setAnimatingColor(ContextCompat.getColor(context, R.color.bg_blue));
    footer.setNormalColor(ContextCompat.getColor(context, R.color.transparent));
    //        footer.setPrimaryColors();
    refreshLayout.setRefreshFooter(footer);


  }

  //设置 Header 为 Material风格
//        refreshLayout.setRefreshHeader(
// new MaterialHeader(context).setShowBezierWave(true));////弹起的球,中间有进度圏
//        refreshLayout.setRefreshHeader(new CircleHeader(context));//弹起的球
//        refreshLayout.setRefreshHeader(new DeliveryHeader(context));//热气球
//        refreshLayout.setRefreshHeader(new DropboxHeader(context));//盒子
//        refreshLayout.setRefreshHeader(new FlyRefreshHeader(context).set);
//        refreshLayout.setRefreshHeader(new FunGameBattleCityHeader(context));//坦克
//        refreshLayout.setRefreshHeader(new FunGameHitBlockHeader(context));//乒乓
//        refreshLayout.setRefreshHeader(new MaterialHeader(context));//类swiprefresh
//        refreshLayout.setRefreshHeader(new PhoenixHeader(context));//房子太阳
//        refreshLayout.setRefreshHeader(new StoreHouseHeader(context).initWithString("loading"));//动态文字
//        refreshLayout.setRefreshHeader(new TaurusHeader(context));//蓝天白云
//        WaveSwipeHeader header = new WaveSwipeHeader(context);
//        header.setColorSchemeColors(ContextCompat.getColor(context, R.color.white),
//                ContextCompat.getColor(context, R.color.bg_gray),
//                ContextCompat.getColor(context, R.color.bg_blue),
//                ContextCompat.getColor(context, R.color.bg_gray_deep),
//                ContextCompat.getColor(context, R.color.bg_yellow));
//        header.setPrimaryColors(ContextCompat.getColor(context, R.color.bg_blue_light));
//        refreshLayout.setRefreshHeader(header);//水滴效果
//        refreshLayout.setRefreshHeader(new BezierRadarHeader(context));//球的进度类似时钟，弹起的球
//        refreshLayout.setRefreshHeader(new ClassicsHeader(context));//传统的
//        refreshLayout.setRefreshHeader(new FalsifyHeader(context));//没图
//        refreshLayout.setRefreshHeader(new FunGameHeader(context));//
//设置 Footer 为 球脉冲
//        refreshLayout.setRefreshFooter(new BallPulseFooter(context).setSpinnerStyle(SpinnerStyle.Scale));
//        ViewUtils.setRefreshTextHeader(context,refreshLayout);

  public static int getMeasuredHeight(View view) {
    int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    view.measure(w, w);
    return view.getMeasuredHeight();
  }

  public static int getMeasuredWidth(View view) {
    int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    view.measure(w, w);
    return view.getMeasuredWidth();
  }

  public static int getTopAndStatusHeight(Activity context, View topBar) {
    int statusHeight = 0;
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2) {
      statusHeight = getStatusBarHeight(context);
      if (statusHeight <= 0) {
        statusHeight = DensityUtil.dip2px(context, 24);
      }
    }
    int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    topBar.measure(w, w);
    Log.e("statusHeight---------", statusHeight + "--------------");
    Log.e("topBar---------", topBar.getMeasuredHeight() + "--------------");
    return statusHeight + topBar.getMeasuredHeight();
  }

  public static int getStatusBarHeight(Activity context) {
    /**
     * 获取状态栏高度——方法2
     * */
    int statusBarHeight2 = -1;
    try {
      Class<?> clazz = Class.forName("com.android.internal.R$dimen");
      Object object = clazz.newInstance();
      int height = Integer.parseInt(clazz.getField("status_bar_height")
          .get(object).toString());
      statusBarHeight2 = context.getResources().getDimensionPixelSize(height);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return statusBarHeight2;
  }

  public static void setHtmlText(TextView tv, String text) {
    if (null == tv) {
      return;
    }

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
      tv.setText(Html.fromHtml(text, Html.FROM_HTML_MODE_LEGACY));
    } else {
      tv.setText(Html.fromHtml(text));
    }
  }

  public static void setText(TextView tv, CharSequence text) {
    if (null == tv) {
      return;
    }
    tv.setText(TextUtil.Text(text));
  }

  public static void setText(TextView tv, String text, boolean canNull) {
    if (null == tv) {
      return;
    }
    if (canNull) {
      tv.setText(TextUtil.Text(text));
      return;
    }
    if (!Util.isNull(text)) {
      tv.setText(text);
    }

  }

  public static void setText(TextView tv, String text, String defaul) {
    if (null == tv) {
      return;
    }
    tv.setText(!Util.isNull(text) ? text : TextUtil.Text(defaul));
  }

  public static void setImage(ImageView iv, int res) {
    if (null == iv || 0 == res) {
      return;
    }
    iv.setImageResource(res);
  }

  public static void setImageByUrl(ImageView iv, String url) {
    if (null == iv) {
      return;
    }
    ImageLoaderUtil.getImageLoader().displayImage(url, iv, ImageLoaderUtil.getOptions());
  }

  public static void setImageBitmap(ImageView iv, Bitmap bitmap) {
    if (null == iv || null == bitmap) {
      return;
    }
    iv.setImageBitmap(bitmap);
  }

  public static void setClick(View view, View.OnClickListener listener) {
    if (null == view || null == listener) {
      return;
    }
    view.setOnClickListener(listener);
  }

  public static void setBackgroundColor(Context context, View view, int color) {
    if (null == view || 0 == color) {
      return;
    }
    view.setBackgroundColor(ContextCompat.getColor(context, color));
  }

  @SuppressLint("NewApi")
  public static void setBackground(Context context, View view, int res) {
    if (null == view || 0 == res) {
      return;
    }
    view.setBackground(ContextCompat.getDrawable(context, res));
  }

  public static void setTextColor(TextView view, int color) {
    if (null == view) {
      return;
    }
    view.setTextColor(ContextCompat.getColor(App.getContext(), color));
  }

  public static void setLayoutParams(View view, int width, int height) {
    if (null == view) {
      return;
    }
    try {
      ViewGroup.LayoutParams vl = view.getLayoutParams();
      vl.width = width;
      vl.height = height;
      view.setLayoutParams(vl);
    } catch (Exception e) {

    }
  }

  public static void setMarginFrame(View view, int left, int top, int right, int bottom) {
    try {
      FrameLayout.LayoutParams fl = (FrameLayout.LayoutParams) view.getLayoutParams();
      fl.setMargins(left, top, right, bottom);
    } catch (Exception e) {

    }
  }

  public static View inflate(Context act, int layoutId) {
    try {
      return LayoutInflater.from(act).inflate(layoutId, null);
    } catch (Exception e) {
      return null;
    }
  }

  public static void loadWebCode(WebView webView, String codes) {
    if (Util.isNull(codes) || null == webView) {
      return;
    }
    webView.loadDataWithBaseURL(null, codes,
        "text/html", "utf-8", null);
  }
  public static void setLlLayoutParams(View view, int width, int height) {
    if (null == view || !(view.getParent() instanceof LinearLayout)) {
      return;
    }
    try {
      LinearLayout.LayoutParams ll = (LayoutParams) view.getLayoutParams();
      ll.width = width;
      ll.height = height;
      view.setLayoutParams(ll);
    } catch (Exception e) {

    }
  }

}
